<%@page import="java.util.Date,java.util.Map,java.util.HashMap,java.util.Map.Entry"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		final Map<String, String> cookieList = new HashMap<String, String>();
		for (Cookie cookie : request.getCookies()) {
			cookieList.put(cookie.getName(), cookie.getValue());
		}
		
		final String newCookieName = request.getParameter("newCookieName");
		final String newCookieValue = request.getParameter("newCookieValue");
		if (newCookieName != null && !newCookieName.isEmpty()) {
		    final Cookie newCookie = new Cookie(newCookieName, newCookieValue); 
		    response.addCookie(newCookie);
		    cookieList.put(newCookie.getName(), newCookie.getValue());
		}
		
	    final Cookie lastVisitCookie = new Cookie("lastVisit", new Date().toString());
		response.addCookie(lastVisitCookie);
		cookieList.put(lastVisitCookie.getName(), lastVisitCookie.getValue());
	%>
	
	<form action="" method="post">
		<p>
			<label for="newCookieName">Cookie Name:</label>
			<input id="newCookieName" name="newCookieName" type="text" />
		</p>
		<p>
			<label for="newCookieValue">Cookie Value:</label>
			<input id="newCookieValue" name="newCookieValue" type="text" />
		</p>
		<input type="submit" value="Submit" />
	</form>
	
	<table>
		<tr>
			<th>Cookie name</th>
			<th>Cookie value</th>
		</tr>
		<%
			for (Entry<String, String> entry : cookieList.entrySet()) {
		%>
				<tr>
					<td><%= entry.getKey() %></td>
					<td><%= entry.getValue() %></td>
				</tr>
		<%	    
			}
		%>
	</table>
</body>
</html>